<template>
    <div class="wrap">
        <b-list v-for="(item,index) in list" :key="index"
            :obj="item"
        ></b-list>
    </div>
</template>
<script>
import bList from '../components/bList'
export default {
    components:{
        bList
    },
    data(){
        return {
            list:[],
            addList:[]
        }
    },
    created(){
        this.getData();
        this.$bus.$on('add',(state,id) => {
            let ind = this.list.findIndex(item => item.id === id);
            this.list[ind].state = state;
            this.addList.push(this.list[ind]);
            localStorage.setItem('addList',JSON.stringify(this.addList));
        })
    },
    methods:{
        getData(){
            this.addList = JSON.parse(localStorage.getItem('addList')) || [];
            this.$http.get('/api/list').then(res => {
                if(res.data.code === 1){
                    let data = res.data.data.data.map(item => {
                        item.state = false;
                        this.addList.forEach(aItem => {
                            if(aItem.id === item.id){
                                item.state = aItem.state;
                            }
                        })
                        return item
                    })
                    this.list = data;
                }
            }).catch(error =>{
                console.warn(error)
            })
        }
    }
}
</script>
